import { createRouter, createWebHistory } from 'vue-router'
import store from '../store';
import { Toast,Notify } from 'vant'
const Home = () => import('../views/home/Home')
const Category = () => import('../views/category/Category')
const Detail = () => import('../views/detail/Detail')
const Profile = () => import('../views/profile/Profile')
const ShopCart = () => import('../views/shopcart/ShopCart')
const Register = () => import('../views/profile/Register')
const login = () => import('../views/profile/login')
const Address = ()=> import('../views/profile/Address')
const AddressEdit = ()=> import('../views/profile/AddressEdit')
const routes = [
  {
    path: '',
    name: 'DefaultHome',
    component: Home,
    meta:{
      title:'图书兄弟首页'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta:{
      title:'图书兄弟-首页'
    }
  },
  {
    path: '/category',
    name: 'Category',
    component: Category,
    meta:{
      title:'图书兄弟-商品分类'
    }
  },
  {
    path: '/detail',
    name: 'Detail',
    component: Detail,
    meta:{
      title:'图书兄弟-商品详情'
    }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile,
    
    meta:{
      title:'图书兄弟-个人中心',
      isAuthRequired:true
    }
  },
  {
    path: '/shopcart',
    name: 'ShopCart',
    component: ShopCart,
    meta:{
      title:'图书兄弟-购物车',
      isAuthRequired:true
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta:{
      title:'图书兄弟-用户注册'
    }
  },
  {
    path: '/login',
    name: 'login',
    component: login,
    meta:{
      title:'图书兄弟-登录'
    }
  } ,
  {
    path: '/address',
    name: 'Address',
    component: Address,
    meta:{
      title:'图书兄弟-地址管理',
      isAuthRequired:true
    }
  } ,
  {
    path: '/addressedit',
    name: 'AddressEdit',
    component: AddressEdit,
    meta:{
      title:'图书兄弟-地址编辑',
      isAuthRequired:true
    }
  },
  {
    path: '/createorder',
    name: 'CreateOrder',
    component: ()=>import('../views/order/CreateOrder'),
    meta:{
      title:'图书兄弟-订单预览',
      isAuthRequired:true
    }
  },
  {
    path: '/orderdetail',
    name: 'OrderDetail',
    component: ()=>import('../views/order/OrderDetail'),
    meta:{
      title:'图书兄弟-订单详情',
      isAuthRequired:true
    }
  },
  {
    path: '/order',
    name: 'Order',
    component: ()=>import('../views/order/Order'),
    meta:{
      title:'图书兄弟-订单列表',
      isAuthRequired:true
    }
  }

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 跳转之前
router.beforeEach((to,from,next)=>{
  // 判断是否有登录,在这里到login
  // 需要授权 && 判断是否有没有登录
  if(to.meta.isAuthRequired && store.state.user.isLogin === false){
    Notify('您还没有登录，请先登录');
    return next('/login');
  }else{
    next();
  }
  // 设置导航的标题栏
  document.title = to.meta.title;
})

export default router
